{include file="../application/admin/view/header.html" /}
</head>
<body>
<div class="container">
	<div class="row">
	<div class="col-md-12">
		<div class="alert kesn_prompt"></div>
	</div>
		<button class="btn btn-info add_submodules">新增子模块<span class="fui-plus btn-tip"></span></button>
	</div><br/>
	<div class="row input-group col-md-4">
	  <span class="input-group-addon">模块名</span>
	  <input type="text" class="form-control" name="module" placeholder="" disabled value="{$module.module}" data-_module_id="{$module.id}"/>
	</div>
	&nbsp;&nbsp;<br/>
	<div class="row">
		<table class="table table-hover">
			<thead>
				<th>ID</th>
				<th class="col-sm-2 col-md-2">子模块</th>
				<th class="col-sm-2 col-md-2">data-id</th>
				<th>url</th>
				<th>操作</th>
			</thead>
			<tbody>
			{volist name="submodules" id="submodule"}
			<tr class="_item">
				<th scrope="row">{$submodule.id}</th>
				<td><input type="text" class="form-control input-sm" name="submodule" placeholder="" disabled value="{$submodule.submodule}"/></td>
				<td><input type="text" class="form-control input-sm" name="data_id" placeholder="" disabled value="{$submodule.data_id}"/></td>
				<td><input type="text" class="form-control input-sm" name="url" placeholder="" disabled value="{$submodule.url}"/></td>
				<td><div>
					<a href="#" class="btn btn-primary kesn_edit btn-sm">编辑<span class="fui-new btn-tip"></span></a>
					<a href="#" class="btn btn-danger kesn_del btn-sm" data-_id="{$submodule.id}">删除<span class="fui-cross btn-tip"></span></a>
				</div></td>
			</tr>
			{/volist}
		</tbody>
		</table>
	</div>
	{$page}
	<button class="btn btn-info module_save">保存</button>

</div>

{include file="../application/admin/view/footer.html" /}
<script type="text/javascript">
//新增子模块
$('.container').on('click','.add_submodules',function(){
	var _html ='<tr class="_item"><th scrope="row"></th><td><input type="text" class="form-control input-sm" name="submodule" placeholder="" /></td><td><input type="text" class="form-control input-sm" name="data_id" placeholder="" /></td><td><input type="text" class="form-control input-sm" name="url" placeholder="" /></td><td><div><a href="#" class="btn btn-danger kesn_del btn-sm" data-_id="new">删除<span class="fui-cross btn-tip"></span></a></div></td></tr>';
	$('tbody').append(_html);
});
//删除
$('tbody').on('click','.kesn_del',function(){
	var _this = $(this);
	var _id = _this.attr('data-_id');
	if (_id == 'new') {
		_this.parents('._item').remove();
		return;
	}
	var _name = _this.parents('._item').find('input[name=submodule]').val();
	var _url = 'del_submodule';
	kesn_del(_id,_url,'您确定要删除 '+_id+' : '+_name+' 吗？',_this);
});


$(document).ready(function(){

});
//编辑
$('.kesn_edit').click(function(){
	var _this = $(this);
	_this.parents("._item").find('input').removeAttr('disabled');
});
$('.input-group-addon').click(function(){
	var _this = $(this);
	_this.parent('.row').children('input[name=module]').removeAttr('disabled');
});

//保存
$(".module_save").click(function(){
	var _this = $(this);
	var dataArr = {};
	dataArr['id'] = $("input[name=module]").attr('data-_module_id');
	dataArr['module'] = $("input[name=module]").val();
	if (dataArr['module'] == '') {
		kesn_prompt('fail','模块名不能为空！');
		return;
	}
	dataArr['submodules'] = {};
	$('._item').each(function(i){
		var this_i = $(this);
		var arr_tmp = {};
		arr_tmp['id'] = this_i.find('th[scrope=row]').text();
		arr_tmp['submodule'] = this_i.find('input[name=submodule]').val();
		arr_tmp['data_id'] = this_i.find('input[name=data_id]').val();
		arr_tmp['url'] = this_i.find('input[name=url]').val();
		dataArr['submodules'][i] = arr_tmp;
	});

	$.ajax({
			url : 'edit',
			async:true,
			type:'POST',
			data:dataArr,
			dataType:'JSON',
			success:function(data,textStatus,jqXHR){
    				if (data.err != 0) {
    					kesn_prompt('fail',data.content);
    				}else if (data.err == 0) {
                        kesn_prompt('success',data.content);
                        _this.attr('disabled','disabled');

    				}
				},
    			error:function(){
    				kesn_prompt('fail','网络出错');
    			}
		});
});
</script>
</footer>
</body>
</html>